// 引入coolsite360交互配置设定
require('coolsite.config.js');
import {
  // getIdShowTextFromUserInfo,
  injectUserInfoFromStorageIntoData,
  getNowFormatDate,
} from '../../utils/common';
import {
  leavyFlagDict
} from '../../config';

// 获取全局应用程序实例对象
var app = getApp();

// 创建页面实例对象
Page({
  /**
   * 页面名称
   */
  name: "two",
  /**
   * 页面的初始数据
   */

  data: {
    successInfoBgColor: app.globalData.successInfoBgColor,
    failInfoBgColor: app.globalData.failInfoBgColor,
    warmBgColor: app.globalData.warmBgColor,
    cla: '',
    id: '',
    name: '',
    room: '',
    info: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(option) {
    const that = this;
    injectUserInfoFromStorageIntoData.apply(this, []);

    // 注册coolsite360交互模块
    app.coolsite360.register(this);

    // var role = wx.getStorageSync('role');
    // injectUserInfoFromStorageIntoData.apply(this, []);
    app.editTabBarTeacher2();

    // if (role == '1') {
    //   app.editTabBarTeacher1();
    // } else {
    //   app.editTabBarTeacher2();
    // }
    const leavyid = option.leavyid;

    // var str=wx.getStorageSync("str");
    // var that=this;
    // var leavy_num = parseInt(str.split('_')[0]);
    // console.log(leavy_num);
    // var student_id = str.split('_')[1];
    const leavyDetialApiUrl = `${app.globalData.apiBaseUrl}/read_leavy_deatil_by_leavyid?leavyid=${leavyid}`
    wx.request({
      url: leavyDetialApiUrl,
      // data:{
      //   // student_id:student_id
      // },
      // header: {
      //   "content-type": "application/json"
      // },
      method: "GET",
      success: function (res) {
        console.log(`68-68 teacher/three.js hook:::::onLoad::::success:::res:`, res);
        const record = res.data;
        console.log(`70-70 teacher/three.js hook:::::onLoad::::success:::record:`, record);

        // var json=e.data[0];
        // var leavyinfojson = wx.getStorageSync('leavyinfojson');
        // console.log(leavyinfojson);
        // const needHMS = true;
        // const stime = getNowFormatDate(new Date(Date.parse(record.stime)), needHMS);
        // const stime = getNowFormatDate(new Date(Date.parse(record.stime)));
        // const etime = getNowFormatDate(new Date(Date.parse(record.etime)), needHMS);
        // const etime = getNowFormatDate(new Date(Date.parse(record.etime)));
        const leavyid = record.leavyid
        const stime = record.stime
        const etime = record.etime
        const student_id = record.student_id;
        const flag = record.flag
        const grade = record.grade
        const cla = record.cla
        const name = record.name
        const room = record.room
        const info = record.reason
        const ensure = record.ensure
        that.setData({
          leavyid,
          stime,
          etime,
          flag,
          ensure,
          student_id,
          grade,
          cla,
          name,
          room,
          info,
          ensure,
        });
        that.setNavigationBarTitle(flag);
        // for(var x in leavyinfojson){
        //   if (leavyinfojson[x].leavy_num==leavy_num){
        //     var stime = leavyinfojson[x].start_time;
        //     var etime = leavyinfojson[x].end_time;
        //     var ensure=leavyinfojson[x].ensure;
        //     that.setData({
        //       cla: json.class,
        //       name: json.name,
        //       room: json.room,
        //       id: student_id,
        //       stime: stime,
        //       etime: etime,
        //       info: leavyinfojson[x].reason,
        //       ensure: ensure
        //     })
        //     break;
        //   }
        // }

      }
    })
  },

  setNavigationBarTitle(flag){
    const flagText = leavyFlagDict[flag];
    console.log(`43-43 leavyDetail.js hook:::::onLoad:::flagText:`, flagText, `typeof(flag):${typeof(flag)}, flag:${flag}`);
    const pageTitle = `${flagText}的请假`;
    wx.setNavigationBarTitle({
      title: pageTitle,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 执行coolsite360交互组件展示
    app.coolsite360.onShow(this);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },


  //以下为自定义点击事件
  changeflag: function (event) {
    // var str = wx.getStorageSync("str");
    // var leavy_num = parseInt(str.split('_')[0]);
    const newfalg = event.currentTarget.dataset.newfalg;
    const leavyid = this.data.leavyid;
    wx.request({
      url: `${app.globalData.apiBaseUrl}/teacher/update_leavy`,
      data: {
        leavyid: leavyid,
        flag: newfalg,
      },
      header: {
        "content-type": "application/json"
      },
      method: "post",
      success: function (e) {
        console.log(e.data);
        if (e.data == "True") {
          wx.redirectTo({
            url: '/page/teacher/page/three/three',
          });
          wx.showToast({
            title: '批假成功',
            icon: 'success',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: '批假失败',
            icon: 'fail',
            duration: 2000
          })
        }
      }
    })
  },
  changeflag0: function () {
    var str = wx.getStorageSync("str");
    var leavy_num = parseInt(str.split('_')[0]);
    wx.request({
      url: `${app.globalData.apiBaseUrl}/teacher/update_leavy`,
      data: {
        leavy_num: leavy_num,
        flag: 0
      },
      header: {
        "content-type": "application/json"
      },
      method: "post",
      success: function (e) {
        console.log(e.data);
        if (e.data == "True") {
          wx.redirectTo({
            url: '../one/one',
          })
          wx.showToast({
            title: '批假成功',
            icon: 'success',
            duration: 2000
          });
        } else {
          wx.showToast({
            title: '批假失败',
            icon: 'fail',
            duration: 2000
          })
        }
      }
    })
  },
  preImage: function () {
    console.log("ok");
    var urlArray = new Array();
    var ensure = this.data.ensure;
    wx.previewImage({
      current: ensure,
      urls: [ensure],
      success: function (res) {

      },
      fail: function (res) {},
      complete: function (res) {},
    })
  },
  goBack: function () {
    wx.redirectTo({
      url: '../one/one',
    })
  }
})